#include <Windows.h>
#include <tchar.h>
#include <commctrl.h>
#include "resource.h"
#include <TlHelp32.h>

#define IDT_UPDATEPROC	1000

void Update(HWND hLvRunning)
{
	ListView_DeleteAllItems(hLvRunning);

	HANDLE hSnapProc = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (INVALID_HANDLE_VALUE != hSnapProc)
	{
		PROCESSENTRY32 pe32 = {sizeof(PROCESSENTRY32)};
		if (Process32First(hSnapProc, &pe32))
		{
			do {
				LVITEM lvItem;
				lvItem.mask		= LVIF_TEXT;
				lvItem.iItem	= 0;
				lvItem.iSubItem	= 0;
				lvItem.pszText	= pe32.szExeFile;
				int nItem = ListView_InsertItem(hLvRunning, &lvItem);

				HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pe32.th32ProcessID);
				if (NULL != hProcess)
				{
					TCHAR szTime[MAX_PATH];
					FILETIME CreateTime, CreateTime1, CreateTime2, CreateTime3, StartTime;
					SYSTEMTIME SysTime;
					GetProcessTimes(hProcess, &CreateTime, &CreateTime1, &CreateTime2, &CreateTime3);
					FileTimeToLocalFileTime(&CreateTime, &StartTime);
					FileTimeToSystemTime(&StartTime, &SysTime);
					_stprintf(szTime, _T("%04d-%02d-%02d  %02d:%02d:%02d"),
						SysTime.wYear, SysTime.wMonth, SysTime.wDay,
						SysTime .wHour, SysTime.wMinute, SysTime.wSecond);

					ListView_SetItemText(hLvRunning, nItem, 1, szTime);
					CloseHandle(hProcess);
				}
			} while (Process32Next(hSnapProc, &pe32));
		}

		CloseHandle(hSnapProc);
	}
}

int CALLBACK DlgProc(HWND hDlg, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	static HWND hLvRunning;
	static HWND hLvStopped;

	switch (nMsg)
	{
		case WM_INITDIALOG:
		{
			SetWindowText(hDlg, _T("Demo0083"));

			UINT nWidth		= GetSystemMetrics(SM_CXSCREEN);
			UINT nHeight	= GetSystemMetrics(SM_CYSCREEN);
			RECT rtClient;
			GetClientRect(hDlg, &rtClient);
			SetWindowPos(hDlg, NULL, (nWidth - (rtClient.right - rtClient.left)) / 2, (nHeight - (rtClient.bottom - rtClient.top)) / 2, 0, 0, SWP_NOSIZE);
			
			hLvRunning = GetDlgItem(hDlg, IDC_LVRUNNING);
			hLvStopped = GetDlgItem(hDlg, IDC_LVSTOPPED);
			
			LVCOLUMN lvCol	= {0};
			lvCol.mask		= LVCF_WIDTH|LVCF_TEXT;
			lvCol.cx		= 200;
			lvCol.pszText	= _T("Name");
			ListView_InsertColumn(hLvRunning, 0, &lvCol);
			ListView_InsertColumn(hLvStopped, 0, &lvCol);

			lvCol.cx		= 200;
			lvCol.pszText	= _T("Start Time");
			ListView_InsertColumn(hLvRunning, 1, &lvCol);
			ListView_InsertColumn(hLvStopped, 1, &lvCol);

			lvCol.cx		= 200;
			lvCol.pszText	= _T("End Time");
			ListView_InsertColumn(hLvStopped, 2, &lvCol);

			ListView_SetExtendedListViewStyle(hLvRunning,  LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
			ListView_SetExtendedListViewStyle(hLvStopped, LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);

			Update(hLvRunning);
			//SetTimer(hDlg, 10000, IDT_UPDATEPROC, NULL);

			break;
		}
		case WM_TIMER:
		{
			Update(hLvRunning);

			break;
		}
		case WM_COMMAND:
		{
			break;
		}
		case WM_CLOSE:
		{
			PostQuitMessage(0);
			break;
		}
	}

	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc);

	return TRUE;
}